package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Book;
import com.asm.repository.BookRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.BookVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @param * @param null
 * @author Tanzujie
 * @date 2019-07-10
 * @return
 */
@Service
public class BookService {
    @Autowired
    private BookRepository bookRepository;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取工单信息列表
     *
     * @param page
     * @return
     */
    private List<BookVo> getBookVoList(Page<Book> page) {
        List<BookVo> list = new ArrayList<>();
        for (Book Book : page.getContent()) {
            BookVo bookVo = new BookVo();
            BeanUtils.copyProperties(Book, bookVo);
            list.add(bookVo);
        }
        return list;
    }

    /**
     * 分页查询所有工单信息
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<BookVo> findAllBooks(Integer pageNumber, Integer pageSize) {
        Page<Book> page = bookRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<BookVo>(pageNumber, pageSize, page.getTotalPages(), getBookVoList(page));
    }

    /**
     * 根据id查询工单信息
     *
     * @param id
     * @return
     */
    public BookVo findBookById(Long id) {
        Book Book = bookRepository.findById(id).get();
        BookVo bookVo = new BookVo();
        BeanUtils.copyProperties(Book, bookVo);
        return bookVo;
    }

    /**
     * 根据工人id分页查询工单信息
     *
     * @param workerId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<BookVo> findBookByWorkerId(Long workerId, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<Book> books = bookRepository.findByWorkerId(workerId, page);
        return new PageResult<BookVo>(pageNumber, pageSize, books.getTotalPages(), getBookVoList(books));
    }

    /**
     * 根据合同id分页查询工单信息
     *
     * @param size
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<BookVo> findBookByContractId(Long size, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<Book> books = bookRepository.findByContractId(size, page);
        return new PageResult<BookVo>(pageNumber, pageSize, books.getTotalPages(), getBookVoList(books));
    }

    /**
     * 添加工单信息
     *
     * @param bookVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void addBook(BookVo bookVo) {
        Book Book = new Book();
        BeanUtils.copyProperties(bookVo, Book);
        Book.setId(snowflakeIdWorker.nextId());
        Book.setCreateBy(1L);
        Book.setCreateAt(new Date());
        Book.setUpdateBy(1L);
        Book.setUpdateAt(new Date());
        bookRepository.save(Book);
    }

    /**
     * 更新工单信息
     *
     * @param bookVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBook(BookVo bookVo) {
        Book Book = bookRepository.findById(bookVo.getId()).get();
        BeanUtils.copyProperties(bookVo, Book);
        Book.setUpdateBy(1L);
        Book.setUpdateAt(new Date());
        bookRepository.save(Book);
    }

    /**
     * 根据Id删除工单信息
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }
}
